package com.ruoyi.common.utils.shormsg;

import com.alibaba.fastjson2.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.ruoyi.common.utils.ProjectConstant;

/**
* 发送短信
* @author liuz
*/
public class SmsUtil {

	
  public static SmsSendResult sendSms(String phoneNumber, String code) {
      JSONObject json = new JSONObject();
      json.put("code", code);
      return sendSms(ProjectConstant.REGION_ID, ProjectConstant.ACCESSKEY_ID, ProjectConstant.ACCESSKEY_SECRET,
              phoneNumber, ProjectConstant.SIGN_NAME, ProjectConstant.TEMPLATE_NAME, json.toJSONString());
  }

  public static SmsSendResult sendSms(String regionId, String accessKeyId, String secret, String phoneNumber,
                                      String signName, String templateCode, String templateParam) {
      DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, secret);
      IAcsClient client = new DefaultAcsClient(profile);

      CommonRequest request = new CommonRequest();
//      request.setMethod(MethodType.POST);
      request.setSysMethod(MethodType.POST);
//      request.setDomain("dysmsapi.aliyuncs.com");
      request.setSysDomain("dysmsapi.aliyuncs.com");
//      request.setVersion("2017-05-25");
      request.setSysVersion("2017-05-25");
//      request.setAction("SendSms");
      request.setSysAction("SendSms");
      request.putQueryParameter("RegionId", "cn-hangzhou");
      request.putQueryParameter("PhoneNumbers", phoneNumber);
      request.putQueryParameter("SignName", signName);
      request.putQueryParameter("TemplateCode", templateCode);
      request.putQueryParameter("TemplateParam", templateParam);
      try {
          CommonResponse response = client.getCommonResponse(request);
          System.out.println(response.getData());
          return JSONObject.parseObject(response.getData(), SmsSendResult.class);
      } catch (Throwable e) {
          e.printStackTrace();
          return new SmsSendResult().setCode(e.getMessage());
      }
  }
}
